package com.equipment.management.controller;

import com.equipment.management.entity.Mainframe;
import com.equipment.management.mqtt.MqttGateway;
import com.equipment.management.service.MainframeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/mqtt")
public class MqttController {

    @Autowired
    private MqttGateway mqttGateway;
    @Autowired
    private MainframeService mainframeService;
    @GetMapping("/sendMqtt")
    public String sendMqtt(String message) {
        // 发去stm32
        // 发到主题sub_to_stm32
        List<Mainframe> list=mainframeService.getAllmainframe();
        for(Mainframe m:list){
            log.info("推送主题：{}",m.getCode());

            mqttGateway.sendToMqtt(message, "device_sub:"+String.valueOf(m.getCode()));
            mqttGateway.sendToMqtt(message, "device_pub:"+String.valueOf(m.getCode()));

        }
        return "OK";
    }
}
